Sending Messages via Bluetooth Within a User Program

Once you have two NXTs connected via BT, it is very easy to send messages between them. There are only three functions that are needed:

  1. cCmdMessageWriteToBluetooth writes amessage.
  2. cCmdMessageGetSize the size of the first message in a mailbox containing a queue of received messages.
  3. cCmdMessageRead the first message from a mailbox queue and copies it to a user buffer.

After calling each of the above functions you should check the returned value to determine the success/failure of the function.

Use the function cCmdMessageWriteToBluetooth(nQueueID, nXmitBuffer, nSizeOfMessage)to send a BT message to the far end NXT. Check the error code to make sure message was transmitted successfully. This sends the message (up to 58 bytes in length) in the variablenXmitBufferto queue or mailbox numbernQueueIDon the far end NXT.nSizeOfMessageis the length of the message.

When messages are received over BT by a NXT they are automatically added to the end of one of the 10 message or mailbox queues. Use the function cCmdMessageGetSize(nQueueID) to determine whether any messages have been received. A positive return value indicates that a message was received and is the number of bytes in the message.

Use the function cCmdMessageRead(nQueueID, nRcvBuffer, nSizeOfMessage)to retrieve the first message from the specified mailbox and copy it to buffer atnRcvBuffer. Only the firstnSizeOfMessagebytes of the message are copied.nQueueID is the mailbox number to obtain the message from.

The sample program "NXT BT Messaging No Error Checking.c" is a simple program to show how to use all three of the functions. The source for the sample program is given below.

 
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                    Bluetooth Sample Program
//
// ROBOTC provides easy send and receive of messages over Bluetooth. This sample program illustrate the
// basic concept. Bluetooth error checking has been removed from the program to make it easier to
// follow the program logic.
//
// There are other sample programs in the ROBOTC distribution that include Bluetooth using error
// checking.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma platform(NXT)

long nLastXmitTimeStamp = nPgmTime;
long nDeltaTime         = 0;

const int kTimeBetweenXmit = 30;

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                           Check to See if Bluetooth Link is Connection
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

void checkBTLinkConnected()
{
  if (nBTCurrentStreamIndex >= 0)
    return;

  PlaySound(soundLowBuzz);
  PlaySound(soundLowBuzz);
  eraseDisplay();
  nxtDisplayCenteredTextLine(3, "BT not");
  nxtDisplayCenteredTextLine(4, "Connected");
  wait1Msec(3000);
  StopAllTasks();
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                        Send Messages Task
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

const int kMaxSizeOfMessage = 5;
const int kQueueID = 0;

void sendDataMsg()
{
  ubyte nXmitBuffer[kMaxSizeOfMessage] = {0x01, 0x02, 0x03, 0x04, 0x00};
   // For NXT-G compatability, last byte of message must be zero because of string messsages.
  const bool bWaitForReply = false;
  TFileIOResult nBTCmdErrorStatus;

  nDeltaTime = nPgmTime - nLastXmitTimeStamp;
  if (nDeltaTime < kTimeBetweenXmit)
    return;

  if (bBTBusy)
    return;

  nBTCmdErrorStatus = cCmdMessageWriteToBluetooth(nXmitBuffer, kMaxSizeOfMessage, kQueueID);
  switch (nBTCmdErrorStatus)
  {
  case ioRsltSuccess:
  case ioRsltCommPending:
    nLastXmitTimeStamp = nPgmTime;
    break;

  case ioRsltCommChannelBad:
  default:
    break;
  }
  return;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                        Receive Messages Task
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

void readMultipleDataMsgs()
{
  TFileIOResult nBTCmdRdErrorStatus;
  int nSizeOfMessage;
  ubyte nRcvBuffer[kMaxSizeOfMessage];

  while (true)
  {
    // Check to see if a message is available

    nSizeOfMessage = cCmdMessageGetSize(kQueueID);
    if (nSizeOfMessage <= 0)
    {
      wait1Msec(1);    // Give other tasks a chance to run
      break;           // No more message this time
    }

    if (nSizeOfMessage > kMaxSizeOfMessage)
      nSizeOfMessage = kMaxSizeOfMessage;
    nBTCmdRdErrorStatus = cCmdMessageRead(nRcvBuffer, nSizeOfMessage, kQueueID);

    // Add your code to process the message here.
  }
  return;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//                                        Main Task
//
////////////////////////////////////////////////////////////////////////////////////////////////////////

task main()
{
  bNxtLCDStatusDisplay = true;
  wait1Msec(2000); // Give time to start the program at the far end as well

  //
  // Send and receive 1M messages
  //
  for (long nSendTotal = 0; nSendTotal < 1000000; ++nSendTotal)
  {
    checkBTLinkConnected();
    sendDataMsg();
    readMultipleDataMsgs();
    wait1Msec(1);
  }

  return;
}